home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / KEYBOARD.SWG / 0004_INT09 Keyboard handler #2.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  4KB  |  127 lines

  1. {
  2. Here is my source For the keyboard handler.
  3. }
  4.  
  5. {$X+}
  6.  
  7. Unit KbIO;
  8.  
  9. (*---------------------------*) Interface (*----------------------------*)
  10.  
  11. Uses Dos;
  12.  
  13. Var
  14.    KbScancode  : Byte;  { internal Variable, can be used by host Program }
  15.    OldInt9Vect : Pointer;  { For storing the old interrupt vector }
  16.  
  17. Procedure RestoreOldInt9;
  18. Procedure NewInt9; Interrupt;
  19.  
  20. (*------------------------*) Implementation (*--------------------------*)
  21.  
  22. Procedure RestoreOldInt9;  { Restores control to the old interrupt handler }
  23. begin
  24.   SetIntVec($09, OldInt9Vect);
  25. end;
  26.  
  27. {$F+}
  28. Procedure NewInt9; (* Interrupt; *)
  29. Var
  30.   scancode : Byte;
  31.  
  32.   Procedure ResetKBD;
  33.   Var
  34.     b : Byte;
  35.   begin
  36.        b := Port[$61];
  37.        Port[$61] := b or $80;
  38.        Port[$61] := b;
  39.        Port[$20] := $20;
  40.   end;
  41.  
  42. begin
  43.   scancode   := Port[$60];
  44.   KbScancode := scancode;
  45.   (* at this point, you could add Up, Down, Left & Right Vars
  46.      eg. if (KbScancode = 72) then Up := True;
  47.          if (KbScancode = 72 + 128) then Up := False;
  48.          .
  49.          .
  50.          .
  51.          Don't Forget to initialize Up, Down, etc. if you use them! *)
  52.   ResetKBD;
  53. end;
  54. {$F-}
  55.  
  56. begin
  57.   GetIntVec($09, OldInt9Vect);
  58.   SetIntVec($09, @NewInt9);
  59.   KbScancode := 0;
  60.   (*
  61.     At this point, the Unit could install a custom Exit Procedure
  62.     that automatically restores the old keyboard handler when the
  63.     host Program finishes.
  64.   *)
  65. end.
  66.  
  67. {
  68. Just include this Unit in your Uses clause, and, at any time during your
  69. Program, you can check 'KbScancode' to see which key was currently pressed or
  70. released.  Pressed keys have values between 0..127, and released keys have a
  71. value between 128..255.  ESC = scancode #1, so here's a sample.
  72. }
  73. Function Check4Quit : Boolean;
  74. Var
  75.   kbcode  : Byte;
  76.   tmpBool : Boolean;
  77. begin
  78.   tmpBool := False;
  79.   kbcode := KbScancode;
  80.   if (kbcode = 1) then
  81.   begin
  82.  
  83.      Repeat
  84.        kbcode := KbScancode
  85.      Until (kbcode <> 1);
  86.      (* the above line Repeats Until a different key is pressed
  87.         or released *)
  88.  
  89.      if (kbcode = 129) then
  90.        tmpBool := True;
  91.      (* if they released ESC directly after pressing it, without
  92.         pressing or releasing any other keys, return a True value *)
  93.  
  94.   end;
  95.   Check4Quit := tmpBool;
  96. end;
  97.  
  98. {
  99. So, basically, it's a good idea to save KbScancode in a temporary Variable
  100. beFore doing any checks on it, as it may change if you do this:
  101.  
  102. if (KbScancode = 1) then begin
  103.    Delay(1);
  104.    WriteLn('You pressed key #', KbScancode);
  105. end;
  106.  
  107. In that short Delay, they may have released the key or pressed a new one,so the
  108. value would have changed, and the Program might screw up.
  109.  
  110. Something to add:  Boolean Variables For Up, Down, Left, and Right, For use in
  111. games and such.  See the section in Procedure NewInt9.
  112.  
  113.  
  114. Hey, Drew.  I Forgot one thing in my message about the custom KB handler.
  115. You'll probably receive this message at the same time as the Unit I sent.
  116. Here is the important message:
  117.  
  118. When using the KbIO Unit, at the very end of your Program, include the line
  119. that restores the old int9 vector.  It is a Procedure called 'RestoreOldInt9'.
  120. It may not be Absolutely essential to include this line, but if you don't
  121. restore the old keyboard handler, you might not be able to Type anything when
  122. the Program Exits!  (not so good, huh?)  What to do: you can install a custom
  123. exit Procedure that restores the old int9 vector.  if you don't know how to do
  124. this, quote these lines, or Write to me about "custom Exit Procedures to
  125. restore the old int9 vector," or something like that.  Bye For now.
  126. }
  127.